package com.web.manager.jpush;

/**
 * Created by Administrator on 2017/3/6.
 */

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 返回
 *
 * @author ekoo
 */
public class Response<T> implements Serializable {
    private int status;/**1成功，非1失败**/
    private String error;//错误类型
    private String message;//错误具体内容
    protected  T params;
    private long timestamp;
    public Response failure() {
        this.status=0;
        this.error="fail";
        this.message="失败!";
        return this;
    }
    public Response failure(String error, String message) {
        this.status=0;
        this.error=error;
        this.message=message;
        return this;
    }
    public Response failure(String message) {
        this.status=0;
        this.error="fail";
        this.message=message;
        return this;
    }

    public Response failure(int status, String message) {
        this.status=status;
        this.error="fail";
        this.message=message;
        return this;
    }

    public Response ok() {
        this.status=1;
        this.error="success";
        this.message="成功!";
        return this;
    }
    public Response ok(String message) {
        this.status=1;
        this.error="success";
        this.message=message;
        return this;
    }

    public Response() {
    }

    public Response(int status, String error, String message, T params, long timestamp) {
        this.status = status;
        this.error = error;
        this.message = message;
        this.params = params;
        this.timestamp = timestamp;
    }

    public Response(int status, String error, String message) {
        this.status = status;
        this.error = error;
        this.message = message;
    }

    public Response(int status,  String message) {
        this.status = status;
        this.message = message;
    }

    public static Response custom(){
        Response response=new Response();
        return response;
    }
    public static Response List(){
        Response<List> response=new Response<List>();
        if(response.getParams()==null){
            response.setParams(Lists.newArrayList());
        }
        return response;
    }
    public static Response Map(){
        Response<Map> response=new Response<Map>();
        if(response.getParams()==null){
            response.setParams(Maps.newHashMap());
        }
        return response;
    }
    public static Response Map(String key, Object value){
        Response<Map> response=new Response<Map>();
        if(response.getParams()==null){
            response.setParams(Maps.newHashMap());
            response.getParams().put(key,value);
        }
        return response;
    }
    public static Response Map(Map map){
        Response<Map> response=new Response<Map>();
        if(response.getParams()==null){
            response.setParams(Maps.newHashMap());
            response.getParams().putAll(map);
        }
        return response;
    }
    public Response addParams(T params) {
        this.params = params;
        return this;
    }


    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getError() {
        return StringUtils.defaultString(error);
    }

    public void setError(String error) {
        this.error = error;
    }

    public String getMessage() {
        return StringUtils.defaultString(message);
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getParams() {
        return params;
    }

    public void setParams(T params) {
        this.params = params;
    }

    public long getTimestamp() {
        if(timestamp==0){
            return System.currentTimeMillis();
        }else {
            return timestamp;
        }
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    @Override
    public String toString() {
        return "Response{" +
                "status=" + status +
                ", error='" + error + '\'' +
                ", message='" + message + '\'' +
                ", params=" + params +
                ", timestamp=" + timestamp +
                '}';
    }
}